using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

using SlimDX;
using SlimDX.Direct3D9;

using miciv;
using miciv.Rendering;
using miciv.Geometry;

namespace micivCore.Data
{
    public class UnitRenderable : RenderableBase, IDiffuse, IAlbedoTexture
    {
        protected UnitRenderableInfo m_unitRenderableInfo;
        protected Color4 m_f4Diffuse;
        protected TextureManager.ManagedTexture m_albedoTexture;
        protected Vector3 m_f3Position;
        protected bool m_bDirty;

        public UnitRenderable(CoreContext _coreContext, UnitRenderableInfo _unitRenderableInfo)
            : base(_coreContext)
        {
            this.m_bDirty = false;
            this.m_unitRenderableInfo = _unitRenderableInfo;
            this.Albedo = this.m_unitRenderableInfo.Albedo;
            this.Diffuse = this.m_unitRenderableInfo.Diffuse;
            this.Position = new Vector3(0.0f, 0.0f, -0.1f);
            this.VertexFeeders = this.m_unitRenderableInfo.Geometry.VertexFeeders;
            Module module = this.m_coreContext.GetData<Module>("micivCore.Module");
            this.RestrictedRendererContexts = module.RestrictedRendererContextes;
        }

        public void Release()
        {

        }

        public Vector3 Position
        {
            get
            {
                return this.m_f3Position;
            }
            set
            {
                if (value != this.Position)
                {
                    this.m_f3Position = value;
                    this.m_bDirty = true;
                }
            }
        }

        #region IRenderable Members

        override public IRenderer Renderer
        {
            get
            {
                return this.m_unitRenderableInfo.Renderer;
            }
        }

        override public void UpdateData(float _deltaTime)
        {
            base.UpdateData(_deltaTime);
            if (false != this.m_bDirty)
            {
                Matrix.Translation(ref this.m_f3Position, out this.m_matWorld);
                this.m_bDirty = false;
            }
            this.Renderer.Add(this);
        }

        #endregion

        #region IDiffuse Members

        public SlimDX.Color4 Diffuse
        {
            get
            {
                return this.m_f4Diffuse;
            }
            set
            {
                this.m_f4Diffuse = value;
            }
        }

        #endregion

        #region IAlbedoTexture Members

        public TextureManager.ManagedTexture Albedo
        {
            get
            {
                return this.m_albedoTexture;
            }
            set
            {
                this.m_albedoTexture = value;
            }
        }

        #endregion
    }
}
